//
//  HMStatusOriginalView.swift
//  Black22
//
//  Created by Apple on 16/11/22.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
import SDWebImage
import YYText


//  原创微博视图
class HMStatusOriginalView: UIView {
    
    //  原创微博视图底部约束条件
    var originalViewBottomConstraint: Constraint?
    
    //  视图所对应的ViewModel
    var statusViewModel: HMStatusViewModel? {
        didSet {
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url {
                userImageView.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenLabel.text = statusViewModel?.status?.user?.screen_name
            timeLabel.text = statusViewModel?.timeString
            sourceLabel.text = statusViewModel?.sourceContent
           
            verfiedTypeImageView.image = statusViewModel?.verfiedTypeImage
            memberImageView.image = statusViewModel?.memberImage
            //  微博内容
//            contentLabel.text = statusViewModel?.status?.text
            contentLabel.attributedText = statusViewModel?.originalAttributedStr
            //  先卸载上次约束
            originalViewBottomConstraint?.uninstall()
            if let picUrls = statusViewModel?.status?.pic_urls, picUrls.count > 0 {
                //  有配图
                pictureView.isHidden = false
                //  绑定数据
                pictureView.picUrls = picUrls
                self.snp_updateConstraints(closure: { (make) in
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
            } else {
                //  没有配图
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
            
            
        }
    }
    
    // MARK:    --懒加载
    //  用户头像
    fileprivate lazy var userImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //  用户认证类型等级图片
    fileprivate lazy var verfiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //  用户昵称
    fileprivate lazy var screenLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.darkGray)
    
    //  会员等级
    fileprivate lazy var memberImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //  时间
    fileprivate lazy var timeLabel: UILabel = UILabel(fontSize: 11, textColor: UIColor.orange)
    //  来源
    fileprivate lazy var sourceLabel: UILabel = UILabel(fontSize: 11, textColor: UIColor.lightGray)
    
    //  原创微博内容
    fileprivate lazy var contentLabel: YYLabel = {
//        let label = UILabel(fontSize: 14, textColor: UIColor.darkGray)
        let label = YYLabel()
        //  最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 20
        //  多行显示
        label.numberOfLines = 0
        return label
    }()

    //  配图视图
    fileprivate lazy var pictureView: HMStatusPictureView = {
        let view = HMStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor.white
        addSubview(userImageView)
        addSubview(verfiedTypeImageView)
        addSubview(screenLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        userImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(10)
            make.leading.equalTo(self).offset(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verfiedTypeImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageView.snp_trailing)
            make.centerY.equalTo(userImageView.snp_bottom)
        }
        screenLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView.snp_trailing).offset(10)
            make.top.equalTo(userImageView)
        }
        memberImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(screenLabel.snp_trailing).offset(10)
            make.top.equalTo(screenLabel)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(userImageView)
            make.leading.equalTo(userImageView.snp_trailing).offset(10)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.top.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(userImageView)
            make.top.equalTo(userImageView.snp_bottom).offset(10)
            make.width.equalTo(self).offset(-20)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        //  关键约束 -> 原创微博视图的底部等于原创微博内容视图的底部
        self.snp_makeConstraints { (make) in
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        //  点击高亮执行的闭包
        contentLabel.highlightTapAction = { (containerView: UIView, attributedText: NSAttributedString, range: NSRange, rect: CGRect) -> Void in
            let result = (attributedText.string as NSString).substring(with: range)
            print(result)
        }
        
        
        
    }
  

}
